//加载动画
var fav = false;
function Loading_animation() {
    //当页面加载的时候开始执行
    document.getElementById("top").style.display = "none";
    if(document.readyState == "complete" && fav==true) {
        var load = document.getElementById("tou");
        var x = load.style.display = "none";
        document.getElementById("top").style.display = "block";
        console.log(x);
    }
}


function doSearch() {
    var user_id = $("#user_id").val();
    var user_account = $("#user_account").val();
    var user_email = $("#user_email").val();
    var user_data = $("#user_data").val();
    var url = "merchant?user_id=" + user_id + "&user_name=" + user_account + "&Emailbox=" + user_email +"&recordDate=" + user_data;
    $.ajax({
        url: url,
        type: "get",
        dataType: "json",
        success: function (res) {
            $(".sm-users").empty();
            $(".sm-users").remove();
            for (var i = 0; i <res.length; i++) {
                var user = "user_"+res[i].id;
                var html = "<tr class='sm-users' id='user_"+res[i].id+"'>";
                html += "<td>"+ res[i].id +"</td>";
                html += "<td>"+ res[i].name +"</td>";
                html += "<td>"+ res[i].email +"</td>";
                html += "<td>"+ res[i].address +"</td>";
                html += "<td>"+ res[i].consigneeName +"</td>";
                html += "<td>"+ res[i].datetime +"</td>";
                html += "<td><input id="+res[i].name+" onclick='Delete_user(id)' type='button' value='删除'>" +
                    "<input id="+res[i].name+" onclick='Update_user(id)' type='button' value='修改'></td>"
                html += "</tr>";
                $("#sm-user").append(html);
            }
            console.log(res);
        },
        error: function (res) {
            alert("获取失败，请类型程序员解决!");
        }
    });
}

//查询所有订单信息
function doorder() {
    var url = "merchant_orderListServlet";
    $.ajax({
        url: url,
        type: "get",
        dataType: "json",
        success: function (res) {
            var order = $("#order");
            order.empty();
            for (var i = 0; i < res.length; i++) {
                var tr = document.createElement("tr");

                var td_1 = document.createElement("td");
                td_1.innerText = i+1;

                var td_2 = document.createElement("td");
                td_2.innerText = res[i].ordernumber;

                var td_3 = document.createElement("td");
                td_3.innerText = res[i].aggregate;

                var td_4 = document.createElement("td");
                td_4.innerText = res[i].orderSubmissionTime;

                var td_5 = document.createElement("td");
                td_5.innerText = res[i].receiverAddress;

                var td_6 = document.createElement("td");

                var td_7 = "";
                if (res[i].state == "未付款" || res[i].state == "订单失效" || res[i].state == "已签收"){
                    var input_1 = document.createElement("input");
                    input_1.setAttribute("type","text");
                    input_1.setAttribute("value",res[i].state);
                    input_1.style.textAlign = "center";
                    input_1.setAttribute("disabled","true");
                    td_6.appendChild(input_1);
                    td_6.setAttribute("colspan","2");
                }else {
                    var select = document.createElement("select");
                    select.setAttribute("name",res[i].ordernumber);
                    var option_1 = document.createElement("option");
                    option_1.innerText = res[i].state;
                    option_1.setAttribute("selected","selected");
                    var option_2 = document.createElement("option");
                    option_2.innerText = "已发货";
                    var option_3 = document.createElement("option");
                    option_3.innerText = "已送达未签收";
                    select.appendChild(option_1);
                    select.appendChild(option_2);
                    select.appendChild(option_3);
                    td_6.appendChild(select);

                    td_7 = document.createElement("td");
                    var input = document.createElement("input");
                    input.setAttribute("type","button");
                    input.setAttribute("onclick","amend(id)");
                    input.setAttribute("id",res[i].ordernumber);
                    input.setAttribute("value","修改");
                    td_7.appendChild(input);
                }

                tr.appendChild(td_1);
                tr.appendChild(td_2);
                tr.appendChild(td_3);
                tr.appendChild(td_4);
                tr.appendChild(td_5);
                tr.appendChild(td_6);
                if (td_7 != ""){
                    tr.appendChild(td_7);
                }
                order.append(tr);
            }
        },
        error: function (res) {
            alert("获取失败，请类型程序员解决!");
        }
    });
}

//把查询的所有商品的类型赋值到商品类型下拉框
function type_of_erchandise() {
    var url = "merchant_query_typeServlet";
    $.ajax({
        url: url,
        type: "get",
        dataType: "json",
        success: function (res) {
            var consignee_type = $("#consignee_type");
            for (var i = 0; i <res.length; i++) {
                var option = document.createElement("option");
                option.innerText = res[i];
                consignee_type.append(option);
            }
            console.log(res);
            fav = true;
            Loading_animation();
        },
        error: function (res) {
            alert("获取失败，请联系程序员解决!");
        }
    });
    doorder();
}
//商品表的操作商品按钮的点击事件
function doConsignee(e) {
    var name = $("#consignee_name");
    var type = $("#consignee_type");
    var price = $("#consignee_price");
    var order_id = e;
    alert(order_id);

    var url = "merchant_consignee?consignee_id=" + order_id;
    $.ajax({
        url: url,
        type: "get",
        dataType: "json",
        success: function (res) {
            for (var i = 0; i <res.length; i++) {
                name.val(res[i].name);
                type.val(res[i].type);
                price.val(res[i].price);
            }
            console.log(res);
        },
        error: function (res) {
            alert("获取失败，请联系程序员解决!");
        }
    });
}
//修改订单的点击事件
function amend(e) {
    var msg = "你当前修改的订单号是: "+e+" 吗？？";
    if(confirm(msg)==true) {
        var merchant_state = document.getElementsByName(e)[0].value;
        $.ajax({
            url: "merchant_StateServlet?order_number=" + e + "&merchant_state=" +merchant_state,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res){
                    alert("修改成功！！");
                    location.reload();
                }
            },
            error: function (res) {
                alert("获取失败，请联系程序员解决!");
            }
        });
    }else {
        return false;
    }

}
//添加商品的点击事件
function Determine_commodity() {
    var consignee_name = document.getElementById("consignee_name").value;
    var consignee_type = document.getElementById("consignee_type").value;
    var consignee_price = document.getElementById("consignee_price").value;
    if (consignee_name != "" || consignee_price != "" && consignee_type != ""){
        $.ajax({
            url: "merchant_judge_commodityServlet?consignee_id="+""+"&consignee_name="+consignee_name,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res){
                    alert("你当前添加的商品id获取商品名称已有，请添加新的商品");
                }else {
                    Add_the_goods();
                }
            },
            error: function (res) {
                alert("获取失败，请联系程序员解决!");
            }
        });
    }else {
        alert("添加商品不能为空，请输入完全！");
    }
}
//添加商品的函数
function Add_the_goods() {
    var consignee_name = document.getElementById("consignee_name").value;
    var consignee_type = document.getElementById("consignee_type").value;
    var consignee_price = document.getElementById("consignee_price").value;
    if (consignee_name != "" || consignee_price != "" && consignee_type != "") {
        $.ajax({
            url: "merchant_Add_MerchandiseServlet?consignee_name=" + consignee_name + "&consignee_type=" + consignee_type + "&consignee_price=" + consignee_price,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res) {
                    alert("商品添加成功，请在下面的添加商品图片！！");
                    window.open("merchantSon.jsp", "_blank", 'height=200,width=600,status=yes,top=200,left=400,toolbar=no,menubar=no,location=no');
                } else {
                    return;
                }
            },
            error: function (res) {
                alert("获取失败，请联系程序员解决!");
            }
        });
    }else {
        return;
    }
}

//查询商品的点击事件
function Query_the_goods() {
    var consignee_name = document.getElementById("consignee_name").value;
    var consignee_type = document.getElementById("consignee_type").value;
    var consignee_price = document.getElementById("consignee_price").value;
    $.ajax({
        url: "merchant_inquire_consigneeServlet?consignee_id="+""+"&consignee_name="+consignee_name+"&consignee_type="+consignee_type+"&consignee_price="+consignee_price,
        type: "get",
        dataType: "json",
        success: function (res) {
            var commodity = $("#commodity");
            commodity.empty();
            for (var i = 0; i < res.length; i++) {
                var tr = document.createElement("tr");

                var td_1 = document.createElement("td");
                td_1.innerText = res[i].id;

                var td_2 = document.createElement("td");
                td_2.innerText = res[i].name;

                var td_3 = document.createElement("td");
                td_3.innerText = res[i].type;

                var td_4 = document.createElement("td");
                td_4.innerText = res[i].price;

                var td_5 = document.createElement("td");
                var input = document.createElement("input");
                input.setAttribute("type","button");
                input.setAttribute("value","操作");
                input.setAttribute("onclick","doConsignee(id)");
                input.setAttribute("id",res[i].id);
                td_5.appendChild(input);
                var input_1 = document.createElement("input");
                input_1.setAttribute("type","button");
                input_1.setAttribute("value","删除");
                input_1.setAttribute("onclick","Delete_goods(id)");
                input_1.setAttribute("id",res[i].name);
                td_5.appendChild(input);
                td_5.appendChild(input_1);

                tr.appendChild(td_1);
                tr.appendChild(td_2);
                tr.appendChild(td_3);
                tr.appendChild(td_4);
                tr.appendChild(td_5);
                commodity.append(tr);
            }
        },
        error: function (res) {
            alert("获取失败，请联系程序员解决!");
        }
    });
}
//修改商品的点击事件
function Modify_the_goods() {
    var consignee_name = document.getElementById("consignee_name").value;
    var consignee_type = document.getElementById("consignee_type").value;
    var consignee_price = document.getElementById("consignee_price").value;
    if (consignee_name != "" || consignee_price != "" && consignee_type != "") {
        $.ajax({
            url: "merchant_Modify_goods?consignee_name=" + consignee_name + "&consignee_type=" + consignee_type + "&consignee_price=" + consignee_price,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res) {
                    var msg = "商品修改成功，请问需要修改商品图片吗？？？";
                    if (confirm(msg)==true){
                        window.open("merchantSon.jsp", "_blank", 'height=200,width=600,status=yes,top=200,left=400,toolbar=no,menubar=no,location=no');
                    }else{
                        return false;
                    }
                } else {
                    alert("修改失败，请查看你修改的商品是否存在！！");
                }
            },
            error: function (res) {
                alert("获取失败，请联系程序员解决!");
            }
        });
    }else {
        return;
    }
}

//当前页面所有重置的点击事件
function cx_reset() {
    location.reload();
}

//页面刷新滚动条不变的函数
function scroll_bar() {
    var scrollPos;
    if (typeof window.pageYOffset != 'undefined') {
        scrollPos = window.pageYOffset;
    }
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
        scrollPos = document.documentElement.scrollTop;
    }
    else if (typeof document.body != 'undefined') {
        scrollPos = document.body.scrollTop;
    }
    document.cookie = "scrollTop=" + scrollPos; //存储滚动条位置到cookies中
}

function scrollback() {
    if (document.cookie.match(/scrollTop=([^;]+)(;|$)/) != null) {
        var arr = document.cookie.match(/scrollTop=([^;]+)(;|$)/); //cookies中不为空，则读取滚动条位置
        document.documentElement.scrollTop = parseInt(arr[1]);
        document.body.scrollTop = parseInt(arr[1]);
    }
}

//删除商品的点击事件
function Delete_goods(e) {
    var msg = "你确定要删除商品叫: "+e+" 的吗？？";
    if(confirm(msg)==true){
            $.ajax({
                url: "merchant_Delete_goodsServlet?consignee_name="+ e,
                type: "get",
                dataType: "json",
                success: function (res) {
                    if (res){
                        alert("商品:"+e+"\t删除成功！！");
                        location.reload();
                    }else {
                        return;
                    }
                },
                error: function (res) {
                    alert("获取失败，请联系程序员解决!");
                }
            });
    }else{
        return false;
    }
}

//用户表删除的点击事件
function Delete_user(e) {
    var msg = "你确定要删除用户叫: "+e+" 的吗？？";
    if(confirm(msg)==true){
        $.ajax({
            url: "merchan_Delete_userServlet?user_name="+ e,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res){
                    alert("用户:"+e+"\t删除成功！！");
                    location.reload();
                }else {
                    return;
                }
            },
            error: function (res) {
                alert("获取失败，请联系程序员解决!");
            }
        });
    }else{
        return false;
    }
}

//用户表修改的点击事件把当前用户名全部重新赋值到文本框中
function Update_user(id) {
    var user_class = document.getElementById(id).parentNode.parentNode;
        $.ajax({
            url: "merchant_select_userServlet?user_name="+ id,
            type: "get",
            dataType: "json",
            success: function (res) {
                var user = user_class.getAttribute("id");
                $("#"+user).empty();
                for (var i = 0; i < res.length; i++) {
                    var td_1 = document.createElement("td");
                    td_1.innerText = res[i].id;

                    var td_2 = document.createElement("td");
                    var input_1 = document.createElement("input");
                    input_1.setAttribute("type","text");
                    input_1.setAttribute("value",res[i].name);
                    td_2.appendChild(input_1);

                    var td_3 = document.createElement("td");
                    var input_2 = document.createElement("input");
                    input_2.setAttribute("type","text");
                    input_2.setAttribute("value",res[i].email);
                    td_3.appendChild(input_2);

                    var td_4 = document.createElement("td");
                    var input_4 = document.createElement("input");
                    input_4.setAttribute("type","text");
                    input_4.setAttribute("value",res[i].address);
                    td_4.appendChild(input_4);

                    var td_5 = document.createElement("td");
                    var input_5 = document.createElement("input");
                    input_5.setAttribute("type","text");
                    input_5.setAttribute("value",res[i].consigneeName);
                    td_5.appendChild(input_5);

                    var td_6 = document.createElement("td");
                    var input_6 = document.createElement("input");
                    input_6.setAttribute("type","text");
                    input_6.setAttribute("value",res[i].datetime);
                    td_6.appendChild(input_6);

                    var td_7 = document.createElement("td");
                    var input_7_1 = document.createElement("input");
                    input_7_1.setAttribute("type","button");
                    input_7_1.setAttribute("value","取消");
                    input_7_1.setAttribute("id",res[i].name);
                    input_7_1.setAttribute("onclick","user_cancel(id)");
                    td_7.appendChild(input_7_1);


                    var input_7 = document.createElement("input");
                    input_7.setAttribute("type","button");
                    input_7.setAttribute("value","修改");
                    input_7.setAttribute("id",res[i].name);
                    input_7.setAttribute("onclick","Update_users(id)");
                    td_7.appendChild(input_7);

                    user_class.appendChild(td_1);
                    user_class.appendChild(td_2);
                    user_class.appendChild(td_3);
                    user_class.appendChild(td_4);
                    user_class.appendChild(td_5);
                    user_class.appendChild(td_6);
                    user_class.appendChild(td_7);
                }
            },
            error: function (res) {
                alert("获取失败，请联系程序员解决!");
            }
        });
}
//在次点击修改的时候修改当前用户名的信息
function Update_users(e) {
    var user_class = document.getElementById(e).parentNode;
    var Datetime = user_class.previousSibling;
    var ConsigneeName = Datetime.previousSibling;
    var Address = ConsigneeName.previousSibling;
    var Email = Address.previousSibling;
    var name = Email.previousSibling;

    var user_name = name.firstChild.value;
    var user_Email = Email.firstChild.value;
    var user_Address = Address.firstChild.value;
    var user_ConsigneeName = ConsigneeName.firstChild.value;
    var user_Datetime = Datetime.firstChild.value;
    if (user_name != "" || user_Email != "" || user_Address != "" || user_ConsigneeName != "" || user_Datetime != ""){
        if (user_name != e){
            var msg = "你确定要修改用户账号: "+e+" 的吗？？\t不到万不得已请不要修改用户的账号";
            if(confirm(msg)==true){
                $.ajax({
                    url: "merchant_update_userServlet?user_name="+ e +"&user_Email=" + user_Email +"&user_Address=" + user_Address +"&user_ConsigneeName=" + user_ConsigneeName +"&user_Datetime=" + user_Datetime,
                    type: "get",
                    dataType: "json",
                    success: function (res) {
                        if (res){
                            alert("用户:"+e+"\t修改成功！！");
                            location.reload();
                        }else {
                            return;
                        }
                    },
                    error: function (res) {
                        alert("获取失败，请联系程序员解决!");
                    }
                });
            }else{
                return false;
            }
        }else {
            $.ajax({
                url: "merchant_update_userServlet?user_name="+ e +"&user_Email=" + user_Email +"&user_Address=" + user_Address +"&user_ConsigneeName=" + user_ConsigneeName +"&user_Datetime=" + user_Datetime,
                type: "get",
                dataType: "json",
                success: function (res) {
                    if (res){
                        alert("用户:"+e+"\t修改成功！！");
                        location.reload();
                    }else {
                        return;
                    }
                },
                error: function (res) {
                    alert("获取失败，请联系程序员解决!");
                }
            });
        }
    }else {
        alert("不能为空修改！！！");
    }
}


//取消修改
function user_cancel(id) {
    var user_class = document.getElementById(id).parentNode.parentNode;
        $.ajax({
            url: "merchant_select_userServlet?user_name="+ id,
            type: "get",
            dataType: "json",
            success: function (res) {
                var user = user_class.getAttribute("id");
                $("#"+user).empty();
                for (var i = 0; i < res.length; i++) {
                    var td_1 = document.createElement("td");
                    td_1.innerText = res[i].id;

                    var td_2 = document.createElement("td");
                    td_2.innerText = res[i].name;

                    var td_3 = document.createElement("td");
                    td_3.innerText = res[i].email;

                    var td_4 = document.createElement("td");
                    td_4.innerText = res[i].address;

                    var td_5 = document.createElement("td");
                    td_5.innerText = res[i].consigneeName;

                    var td_6 = document.createElement("td");
                    td_6.innerText = res[i].datetime;

                    var td_7 = document.createElement("td");
                    var input_7_1 = document.createElement("input");
                    input_7_1.setAttribute("type","button");
                    input_7_1.setAttribute("value","删除");
                    input_7_1.setAttribute("id",res[i].name);
                    input_7_1.setAttribute("onclick","Delete_user(id)");
                    td_7.appendChild(input_7_1);


                    var input_7 = document.createElement("input");
                    input_7.setAttribute("type","button");
                    input_7.setAttribute("value","修改");
                    input_7.setAttribute("id",res[i].name);
                    input_7.setAttribute("onclick","Update_user(id)");
                    td_7.appendChild(input_7);

                    user_class.appendChild(td_1);
                    user_class.appendChild(td_2);
                    user_class.appendChild(td_3);
                    user_class.appendChild(td_4);
                    user_class.appendChild(td_5);
                    user_class.appendChild(td_6);
                    user_class.appendChild(td_7);
                }
            },
            error: function (res) {
                alert("获取失败，请联系程序员解决!");
            }
        });
}


